import cv2
import os
import glob

def images_to_video(image_folder, video_path, fps=30):
    images = sorted(glob.glob(os.path.join(image_folder, '*.jpg')))

    if not images:
        print("未找到图片，请检查路径和图片格式")
        return

    frame = cv2.imread(images[0])
    height, width, layers = frame.shape

    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    video = cv2.VideoWriter(video_path, fourcc, fps, (width, height))

    for idx, image in enumerate(images):
        img = cv2.imread(image)
        if img is not None:
            # 添加时间戳或顺序 ID 到图片左上角
            timestamp = os.path.basename(image).split('.')[0]  # 假设文件名包含时间戳
            text = f"Timestamp: {timestamp} | ID: {idx + 1}"
            font = cv2.FONT_HERSHEY_SIMPLEX
            font_scale = 1
            color = (255, 255, 255)  # 白色
            thickness = 2
            position = (10, 30)  # 左上角位置

            # 在图片上绘制文字
            cv2.putText(img, text, position, font, font_scale, color, thickness)

            video.write(img)
        else:
            print(f"警告：无法读取图片 {image}")

    video.release()
    print(f"视频已生成: {video_path}")

# 使用示例
image_folder = '/home/ai/dataset/0331/results/left_reproject_fisheye'
video_path = '/home/ai/dataset/0331/results/video_left/20250331070337.mp4'

image_folder = '/home/ai/dataset/0331/results1/left_reproject_fisheye'
video_path = '/home/ai/dataset/0331/results1/video_left/20250331071655.mp4'
fps = 30

images_to_video(image_folder, video_path, fps)
